<?php
namespace app\index\controller;

use think\Controller;
use think\Cache;
use think\Debug;

/**
 * 
 */
class Monitor extends Controller
{
     private $webUrl = [
         'http://fwzx.wuning.gov.cn/',
         'http://nfb.wuning.gov.cn/',
         'http://scjg.wuning.gov.cn/',
         'http://wjw.wuning.gov.cn/',
         'http://fyb.wuning.gov.cn/',
         'http://slj.wuning.gov.cn/',
         'http://jtys.wuning.gov.cn/',
         'http://nyj.wuning.gov.cn/'
         ];
//    private $webUrl = [
//        'http://yuning.wuning.gov.cn/',
//        'http://gongyeyuan.wuning.gov.cn/',
//        'http://luxi.wuning.gov.cn/',
//        'http://yangzhou.wuning.gov.cn/',
//        'http://luoping.wuning.gov.cn/',
//        'http://luoxi.wuning.gov.cn/',
//        'http://shimenlou.wuning.gov.cn/'
//        ];
    private $htmlDom = NULL;

    public function _initialize()
    {
        Debug::remark('begin');
        $this->htmlDom = new \HtmlParser\ParserDom(); 
    }

    // public function test()
    // {
	//     $len =  mb_strlen($this->webName);	    
	//     return mb_substr($this->webName,-$len,-21);
    // }

    // private function getWebName($webUrl = '') 
    // {
    //     $html = file_get_contents($webUrl);
    //     $htmlDom = new \HtmlParser\ParserDom($html);
    //     //$htmlDom->find('.banner h1',0)->getPlainText();
    //     return $htmlDom->find('title',0)->getPlainText();
    // }

    // private function getCategoryUrl($webUrl = '')
    // {
    //     $html = file_get_contents($webUrl);
    //     $htmlDom = new \HtmlParser\ParserDom($html);
    //     return $htmlDom->find('#mainNav li h4 a',1)->href;
    // }

    // private function getCategoryName($categoryUrl = '')
    // {
    //     $html = file_get_contents($this->webUrl[1].$categoryUrl);
    //     $htmlDom = new \HtmlParser\ParserDom($html);
    //     //获取监测栏目的名称
    //     $categoryNameSelector = 'h3 a';
    //     return $htmlDom->find($categoryNameSelector,0)->getPlainText();
    // }

    public function index()
    {
        // $categoryInfo = Cache::get($this->webUrl[1]);
        // if (!$categoryInfo) {
        // //初始化一个ParserDom的实例$htmlDom
        // $url =$this->webUrl[1].$this->categoryUrl;
        // $html = file_get_contents($url);
        // $htmlDom = new \HtmlParser\ParserDom($html);
        // $categoryInfo['name'] = $this->webName.'>>'.$this->categoryName;
        // //更改$htmlDom为文章列表ul.newsList的实例，并将栏文章列表抓取组装成二维数组
        // $article = [];
        // $articleList = [];
        // $htmlDom->load($htmlDom->find('ul.newsList',0)->node);
        // $li_arr = $htmlDom->find('li[class!=split],li[!class]');

        // foreach ($li_arr as $li) {
        //     $article = [
        //     'title' => $li->find('a',0)->plaintext,
        //     'url' => $li->find('a',0)->href,
        //     'date' => $li->find('span',0)->plaintext,
        //     ];
        //     $articleList[] = $article;
        // }
        // array_walk($articleList,'my_strtotime');
        // $articleList = my_sort($articleList,'date');

        // $updateInterval = update_interval($articleList,10);
        // array_walk($articleList,'my_timetostr');

        // $categoryInfo['updateInterval'] = $updateInterval;
        // $categoryInfo['articleList'] = $articleList;
        // //写入缓存
        // Cache::set($this->webUrl[1],$categoryInfo,3600);
        // }
        foreach ($this->webUrl as $url) {
            $categoryInfo = $this->category_info($url);
            echo $categoryInfo['name'].'<br>';
            dump($categoryInfo['updateInterval']);
            }
        // $categoryInfo = $this->category_info('http://yuning.wuning.gov.cn/');
        // echo $categoryInfo['name'].'<br>';
        // dump($categoryInfo['updateInterval']);

        echo Debug::getRangeTime('begin','end');
        echo "秒";
        echo '<br>';
        echo Debug::getRangeMem('begin','end');
    }

    private  function category_info($webUrl='')
    {
        //读取缓存
        $categoryInfo = Cache::get($webUrl);
        if (!$categoryInfo) {
            $htmlDom = $this->htmlDom;
            $webName = '';
            $categoryUrl = '';
            $categoryName = '';

            $indexHtml = file_get_contents($webUrl);
            //$htmlDom = new \HtmlParser\ParserDom($indexHtml);
            $htmlDom->load($indexHtml);
            //$htmlDom->find('.banner h1',0)->getPlainText();
            //获取网站名称
            $webName =  str_replace(' - 中国武宁网——武宁县委县政府门户网站','',$htmlDom->find('title',0)->getPlainText());
            //获取监测的栏目url
            $categoryUrl = $htmlDom->find('#mainNav li h4 a',1)->href;

            //获取监测栏目的名称
            $categoryHtml = file_get_contents($webUrl.$categoryUrl);
            $htmlDom->load($categoryHtml);
            $categoryNameSelector = 'h3 a';
            $categoryName = $htmlDom->find($categoryNameSelector,0)->getPlainText();
            //组装成网站>>栏目名称
            $categoryInfo['name'] = $webName.'>'.$categoryName;

            //更改$htmlDom为文章列表ul.newsList的实例，并将栏目文章列表抓取组装成二维数组
            $article = [];
            $articleList = [];
            $htmlDom->load($htmlDom->find('ul.newsList',0)->node);
            $li_arr = $htmlDom->find('li[class!=split],li[!class]');
            foreach ($li_arr as $li) {
                $article = [
                'title' => $li->find('a',0)->plaintext,
                'url' => $li->find('a',0)->href,
                'date' => $li->find('span',0)->plaintext,
                ];
                $articleList[] = $article;
            }
            array_walk($articleList,'my_strtotime');
            //二维数组按子数字‘date’键排序
            $articleList = my_sort($articleList,'date');

            $updateInterval = update_interval($articleList,10);
            array_walk($articleList,'my_timetostr');
            //组装最终返回的监测信息
            $categoryInfo['updateInterval'] = $updateInterval;
            $categoryInfo['articleList'] = $articleList;

            //写入缓存
            Cache::set($webUrl,$categoryInfo,3600);
        }
        return $categoryInfo;
    }
}
